package com.sc.nft.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sc.nft.dao.IdcardInfoDao;
import com.sc.nft.dao.ModifyPhoneNumberRecordsDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.entity.IdcardInfo;
import com.sc.nft.entity.ModifyPhoneNumberRecords;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.ModifyPhoneNumberRecordsDTO;
import com.sc.nft.entity.dto.ModifyPhoneNumberRecordsPageDTO;
import com.sc.nft.entity.dto.UserPhonePageVO;
import com.sc.nft.entity.vo.ModifyPhoneNumberRecordsPageVO;
import com.sc.nft.entity.vo.UserTelPageVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.ModifyPhoneNumberRecordsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 张超
 * @description 针对表【sc_modify_phone_number_records(更改手机号码记录表)】的数据库操作Service实现
 * @createDate 2024-06-24 10:47:19
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ModifyPhoneNumberRecordsServiceImpl implements ModifyPhoneNumberRecordsService {

    private final ModifyPhoneNumberRecordsDao modifyPhoneNumberRecordsDao;
    private final IdcardInfoDao idcardInfoDao;
    private final UserInfoDao userInfoDao;


    @Override
    public IPage<UserTelPageVO> userTelRecordPage(UserPhonePageVO request) {
        return idcardInfoDao.userTelRecordPage(request.toMp(), request.getUserTel());
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void userTelUpdateRecordUpdate(ModifyPhoneNumberRecordsDTO request) {
        //获取用户是否存在
        UserInfo userInfoDaoById = userInfoDao.getById(request.getId());
        IdcardInfo idcardInfo = idcardInfoDao.findLastOneByUserId(request.getId());
        Assert.isFalse(ObjectUtil.isNull(userInfoDaoById), () -> new GlobalRunTimeException("用户不存在！"));

        //修改的手机号不能是已经被使用的
        Assert.isFalse(ObjectUtil.isNotNull(userInfoDao.getOneByUserTel(request.getChangeAfterUserTel())),()->new GlobalRunTimeException("该手机号已被使用"));

        //修改
        Assert.isTrue(userInfoDao.updateUserTelById(request.getId(), request.getChangeAfterUserTel()), () -> new GlobalRunTimeException("修改失败！"));

        //记录
        ModifyPhoneNumberRecords modifyPhoneNumberRecords = new ModifyPhoneNumberRecords();
        modifyPhoneNumberRecords.setUserId(userInfoDaoById.getId());
        modifyPhoneNumberRecords.setChangeBeforeUserTel(userInfoDaoById.getUserTel());
        modifyPhoneNumberRecords.setChangeAfterUserTel(request.getChangeAfterUserTel());
        modifyPhoneNumberRecords.setUserName(idcardInfo.getName());
        modifyPhoneNumberRecords.setCertificateNo(idcardInfo.getCardId());
        modifyPhoneNumberRecords.insert();

    }

    @Override
    public IPage<ModifyPhoneNumberRecordsPageVO> userTelUpdateRecordPage(ModifyPhoneNumberRecordsPageDTO request) {
        return modifyPhoneNumberRecordsDao.userTelUpdateRecordPage(request.toMp(), request.getChangeBeforeUserTel(), request.getChangeAfterUserTel());
    }

}




